###################################################
## 1、得到pyclone的输入
rm -rf ${Pyclone_path}/Input_CiteUp
mkdir -p ${Pyclone_path}/Input_CiteUp
for samples in ` cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list | grep -v Normal ` 
do
echo $samples
Tumor=` echo $samples | awk -F, '{print$1}' `
Normal=` echo $samples | awk -F, '{print$2}' `
Class=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${Tumor} | awk '{print $5}'`
sh ${scripts_path}/pyclone/Pyclone_1_GetInput.Citeup.sh ${Tumor} ${Normal} ${Class}
done

###################################################
## 2、运行Pyclone
export Type=CiteUp
mkdir -p ${Pyclone_path}/result/${Type}
for Normal in ` cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list | grep -v Normal | awk -F, '{print $2}' | sort -u ` 
do
echo " sh ${scripts_path}/pyclone/Pyclone_2_Run.Citeup.sh ${Normal} ${config_path} ${Type}" | \
qsub -N ${Normal}_"Pyclone" -l nodes=1:ppn=4,mem=10gb,walltime=240:00:00 -q batch -d ${Qsub_log_path}
done

###################################################
## 3、描述克隆聚类
## 同时得到citeup的输入
## 完成cluster质控（满足任一）
##	1.	超过4个突变
##	2.	有突变在已报道或我们鉴定的驱动基因上
##	3.	这个cluster的CCF在至少两个样本中最高
## subclone设置为0，不再用CCF过滤，当最后保留的cluster不足两个时候，无法构建亚克隆演化图谱
export Type=CiteUp
cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list | grep -v Normal | awk -F, '{print $2}' | sort -u | xargs -i -P 20 sh -c '
echo {}
${Rscript} ${scripts_path}/pyclone/Pyclone_3_plot.R \
--work_dir ${work_dir} \
--Sample {} \
--out_dir ${Pyclone_path}/CloneImages \
--loci_file ${Pyclone_path}/result/${Type}/{}/tables/loci.tsv \
--cluster_file ${Pyclone_path}/result/${Type}/{}/tables/cluster.tsv \
--clone_t 0.6 \
--subclone_t 0 \
--important_geneList ${work_dir}/public_ref/importTantGene.list
'

finishe_Sample=` ls ${Pyclone_path}/Citeup_subClone | grep cluster  | awk -F'_cluster' '{print $1}' | tr '\n' '|' | sed 's/|$//' `
cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list  | grep -v -E -w ${finishe_Sample}
## 2个样本共享突变太少，且无共享关键驱动突变，无满足只看的cluster
## JZ596B
## JZ700B

###################################################
## 4、运行citup
## 看在胃癌中存在cluster的情况
export inputdir=${Pyclone_path}/Citeup_subClone
export outpath=${Pyclone_path}/Citeup_subClone_result

mkdir -p ${outpath}

for Normal in ` cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list | grep -v Normal | awk -F, '{print $2}' | sort -u  ` 
do
rm -rf ${outpath}/${Normal}_pipe
rm -rf ${outpath}/${Normal}_tmp
rm -rf ${outpath}/${Normal}_Citeup.h5.tmp

echo " sh ${scripts_path}/pyclone/Pyclone_4_citeup.sh ${Normal} ${config_path} ${inputdir} ${outpath}" | \
qsub -N ${Normal}_"citeup" -l nodes=1:ppn=4,mem=20gb,walltime=240:00:00 -q batch -d ${Qsub_log_path}
done

